package net.maku.im.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.im.constant.ImConstant;
import net.maku.im.convert.ImGroupMemberConvert;
import net.maku.im.dao.ImGroupMemberDao;
import net.maku.im.dto.ImGroupMemberQueryDTO;
import net.maku.im.entity.ImGroupMemberEntity;
import net.maku.im.service.ImGroupMemberService;
import net.maku.im.vo.ImGroupMemberVO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 群组成员
 *
 * @author Blue
 * @since 1.0.0 2024-07-14
 */
@Service
@AllArgsConstructor
@DS(ImConstant.DS_IM_PLATFORM)
public class ImGroupMemberServiceImpl extends BaseServiceImpl<ImGroupMemberDao, ImGroupMemberEntity>
    implements ImGroupMemberService {

    @Override
    public PageResult<ImGroupMemberVO> page(ImGroupMemberQueryDTO query) {
        LambdaQueryWrapper<ImGroupMemberEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ImGroupMemberEntity::getGroupId, query.getGroupId());
        wrapper.eq(ImGroupMemberEntity::getQuit, false);
        if (StrUtil.isNotEmpty(query.getShowNickName())) {
            wrapper.and(w1 -> w1.like(ImGroupMemberEntity::getRemarkNickName, query.getShowNickName())
                .or(w2 -> w2.eq(ImGroupMemberEntity::getRemarkNickName, Strings.EMPTY)
                    .and(w3 -> w3.like(ImGroupMemberEntity::getUserNickName, query.getShowNickName()))));

        }
        if (CollectionUtil.isNotEmpty(query.getCreatedTime()) && query.getCreatedTime().size() > 1) {
            wrapper.between(ImGroupMemberEntity::getCreatedTime, query.getCreatedTime().get(0),
                query.getCreatedTime().get(1));
        }
        wrapper.orderByDesc(ImGroupMemberEntity::getId);
        IPage<ImGroupMemberEntity> page = baseMapper.selectPage(getPage(query), wrapper);
        List<ImGroupMemberVO> vos = page.getRecords().stream().map(m -> {
            ImGroupMemberVO vo = ImGroupMemberConvert.INSTANCE.convert(m);
            vo.setShowNickName(StrUtil.isEmpty(m.getRemarkNickName()) ? m.getUserNickName() : m.getRemarkNickName());
            return vo;
        }).collect(Collectors.toList());
        return new PageResult<>(vos, page.getTotal());
    }

    @Override
    public Long findCountByGroupId(Long groupId) {
        LambdaQueryWrapper<ImGroupMemberEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ImGroupMemberEntity::getGroupId, groupId);
        wrapper.eq(ImGroupMemberEntity::getQuit, false);
        return this.count(wrapper);
    }
}